﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="UserMonitorOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" ShowAddButton="false" Title="@($"{_name}列表")" SortByItem="@nameof(UserMonitorOverviewModel.CreateTime)" @ref="dataTableCard"
                   LoadingText="找不到数据欸......不过你可以到词条页面添加监视，点击编辑按钮就能看到入口哦~">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <MIcon Color="info"
                       Class="mr-2"
                       OnClick="()=>LookItem(context.Item)">@IconType.Look.ToIconString()</MIcon>
                <MIcon Color="error"
                       Class="mr-2"
                       OnClick="()=>DeleteItem(context.Item)">@IconType.Delete.ToIconString()</MIcon>
            }
            else if (context.Header.Value == nameof(UserMonitorOverviewModel.Type))
            {
                <TypeChip Type="context.Item.Type" />
            }
            else if (context.Header.Value == nameof(UserMonitorOverviewModel.CreateTime))
            {
                @context.Item.CreateTime.ToString("yyyy-MM-dd HH:mm")
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>



@code {
    private bool _editDialog;
    private long _editedItemId;
    private bool _detailDialog;

    private string _baseUrl = "api/examines/ListUserMonitors";
    private string _name = "关注的词条";

    private List<DataTableHeader<UserMonitorOverviewModel>> _headers = new List<DataTableHeader<UserMonitorOverviewModel>>
    {
          new (){ Text= "添加时间", Value= nameof(UserMonitorOverviewModel.CreateTime)},
          new (){ Text= "名称", Value= nameof(UserMonitorOverviewModel.EntryName),Sortable=false},
          new (){ Text= "类型", Value= nameof(UserMonitorOverviewModel.Type),Sortable=false},        
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<UserMonitorOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public void LookItem(UserMonitorOverviewModel model)
    {
        NavigationManager.NavigateTo($"/entries/index/{model.Id}");
    }

    public async Task DeleteItem(UserMonitorOverviewModel model)
    {
        const string operationName = "取消关注词条";
        try
        {
            var obj = await _httpService.PostAsync<EditUserMonitorsModel, Result>("api/examines/EditUserMonitors", new EditUserMonitorsModel { Ids = new int[] { model.EntryId }, InMonitor = false });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
